<template>
  <div class="login_container">
    <div class="login_box">
      <h2>Admin Login</h2>
      <el-form ref="form" :model="form" :rules="rules">
        <el-form-item prop="username">
          <el-input v-model="form.username" prefix-icon="iconfont icon-user"></el-input>
        </el-form-item>
        <el-form-item prop="password">
          <el-input v-model="form.password" type="password" prefix-icon="iconfont icon-3702mima"></el-input>
        </el-form-item>
        <el-form-item class="submit_btn">
          <el-button type="primary" @click="login">登录</el-button>
          <el-button type="info" @click="resetForm">重置</el-button>
        </el-form-item>
      </el-form>
    </div>
  </div>
</template>

<script>
export default {
  data() {
    return {
      form: {
        username: "admin",
        password: "123456"
      },
      //表单验证规则
      rules: {
        username: [
          { required: true, message: "请输入用户名", trigger: "blur" },
          { min: 3, max: 10, message: "长度在 3 到 10 个字符", trigger: "blur" }
        ],
        password: [
          { required: true, message: "请输入密码", trigger: "blur" },
          { min: 6, max: 15, message: "长度在 6 到 15 个字符", trigger: "blur" }
        ]
      }
    };
  },
  methods: {
    login() {
      this.$refs.form.validate(async (boolean, object) => {
        // console.log(boolean,object)
        if (!boolean) return;
        // this.$axios.post("login",this.form).then(res=>{
        //   console.log(res)
        // })
        const { data: res } = await this.$axios.post("login", this.form);
        if (res.meta.status !== 200) return  this.$message.error("登录失败！");
         this.$message.success("登录成功！");
        console.log(res.data);
        window.sessionStorage.setItem("token", res.data.token);
        this.$router.push("/home");
      });
    },
    resetForm() {
      // console.log(this)
      this.$refs.form.resetFields();
      this.$message.success("重置成功！");
    }
  }
};
</script>

<style lang="less" scoped>
.login_container {
  background-color: #2b4b6b;
  height: 100%;
  position: relative;

  .login_box {
    width: 450px;
    height: 300px;
    background: white;
    position: absolute;
    left: 50%;
    top: 50%;
    transform: translate(-50%, -60%);
    border-radius: 5px;
    padding: 20px;
    box-sizing: border-box;

    h2 {
      text-align: center;
      padding-bottom: 15px;
    }

    .submit_btn {
      text-align: right;
    }
  }
}
</style>